package com.xhwl.centiotdata.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.mq.producer.GeneralProducer;
import com.xhwl.centiotdata.service.cent.IDeviceCentService;
import com.xhwl.common.constant.cent.alarm.AlarmConstant;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.enums.OrganizationTypeEnum;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.alarm.AlarmManagement;
import com.xhwl.common.query.cent.device.DevicePageQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * 处理华为人脸抓拍结果
 *
 * @author pengjunming
 * @description
 * @date 2022/6/7 14:40
 */
@RestController
@RequestMapping("/huawei-msg")
@Slf4j
public class HuaWeiIvs3800Controller {
    @Resource
    private IDeviceCentService iCentDeviceService;

    @Autowired
    GeneralProducer generalProducer;

    @PostMapping(value = "/receive/{projectId}")
    public ResultJson hikReceiveFaceCatch(@RequestBody String data, @PathVariable String projectId) {
        log.info("华为IVS3800人脸抓拍数据:{},projectId:{}", data, projectId);
        handleMessage(data, projectId);
        return ResultJson.success();
    }

    void handleMessage(String xmlMsg, String checkProjectId) {
        JSONObject result = JSONObject.parseObject(xmlMsg, JSONObject.class);
        JSONArray data = (JSONArray) result.get("data");
        if (Objects.nonNull(data)) {
            log.info("data:{}", data);
            for (int i = 0; i < data.size(); i++) {
                JSONObject o = (JSONObject) data.get(i);
                JSONObject commonInfo = (JSONObject) o.get("commonInfo");
                String faceId = commonInfo.getString("blacklistId");
                if (Objects.nonNull(faceId)) {
                    //找到对应的数据后，下单
                    DeviceDTO device = getDevice(Integer.parseInt(checkProjectId), commonInfo.getString("cameraId"));
                    if (device != null) {
                        AlarmManagement alarmManagement = new AlarmManagement();
                        //组装公共部分数据
                        alarmManagement.setProjectId(Integer.parseInt(checkProjectId));
                        alarmManagement.setAlarmTime(new Date());
                        alarmManagement.setDeviceId(device.getId());
                        //海康门禁离线告警
                        alarmManagement.setAlarmInventoryCode("91056465");
                        generalProducer.send(AlarmConstant.DAHUA_ALARM_TOPIC, alarmManagement);
                    }
                }
            }
        } else {
            log.warn("回调数据异常");
        }
    }



    private DeviceDTO getDevice(Integer projectId, String thirdDeviceId) {
        DevicePageQuery devicePageQuery = new DevicePageQuery();
        devicePageQuery.setProjectId(projectId);
        devicePageQuery.setThirdDeviceId(thirdDeviceId);
        devicePageQuery.setSize(1L);
        devicePageQuery.setCurrent(1L);
        List<Integer> types = new ArrayList<>();
        types.add((int) OrganizationTypeEnum.BUILDING.id);
        types.add((int) OrganizationTypeEnum.FLOOR.id);
        types.add((int) OrganizationTypeEnum.ROOM.id);
        types.add((int) OrganizationTypeEnum.AREA.id);
        types.add((int) OrganizationTypeEnum.PARKING_LOT.id);
        types.add((int) OrganizationTypeEnum.PASSAGE.id);
        types.add((int) OrganizationTypeEnum.CAR_SPOT.id);
        types.add((int) OrganizationTypeEnum.PUBLIC_AREA.id);
        types.add((int) OrganizationTypeEnum.PARKING_AREA.id);
        types.add((int) OrganizationTypeEnum.UNIT.id);
        types.add((int) OrganizationTypeEnum.BLOCK.id);
        types.add((int) OrganizationTypeEnum.BALCONY.id);
        devicePageQuery.setTypes(types);
        List<DeviceDTO> deviceDTOList = iCentDeviceService.listDevice(null, devicePageQuery);
        if (Objects.nonNull(deviceDTOList) && deviceDTOList.size() == 1) {
            return deviceDTOList.get(0);
        }
        return null;
    }

}


